package com.useeinfo.oa.modules.security.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.security.entity.UserRoleEnterprise;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

@Repository("UserRoleEnterpriseDao")
public class UserRoleEnterpriseDao implements CrudDao<UserRoleEnterprise> {
    private final static Logger logger = LoggerFactory.getLogger(UserRoleEnterpriseDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    // ******************************************************************************
    // ******************************* 代码自动生成开始 *******************************
    // ******************************************************************************

    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {

        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (StringUtils.isNotBlank(enterpriseId)) {
            conditionHash.put("enterprise.id = ?{paramIndex} ", StringConverters.ToLong(enterpriseId));
        }
        String roleId = queryHash.get("roleId");
        if (StringUtils.isNotBlank(roleId)) {
            conditionHash.put("systemRole.id = ?{paramIndex} ", StringConverters.ToLong(roleId));
        }
        String userId = queryHash.get("userId");
        if (StringUtils.isNotBlank(userId)) {
            conditionHash.put("systemUser.id = ?{paramIndex} ", StringConverters.ToLong(userId));
        }
        String organizations = queryHash.get("organizations");
        if (StringUtils.isNotBlank(organizations)) {
            Set<Long> organizationsSet = new HashSet<>();
            String[] organizas = organizations.split(",");
            for (String organiza : organizas) {
                organizationsSet.add(StringConverters.ToLong(organiza));
            }
            conditionHash.put("and EXISTS(SELECT organ FROM u.systemUser.systemUserOrganizationSet organ WHERE organ.organization.id in (?{paramIndex})) ", organizationsSet);
        }
        String positions = queryHash.get("positions");
        if (StringUtils.isNotBlank(positions)) {
            Set<Long> positionSet = new HashSet<>();
            String[] posits = positions.split(",");
            for (String posit : posits) {
                positionSet.add(StringConverters.ToLong(posit));
            }
            conditionHash.put("and EXISTS(SELECT position FROM u.systemUser.positionSet position WHERE position.id in(?{paramIndex})) ", positionSet);
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(u) from UserRoleEnterprise u ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<UserRoleEnterprise> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select u from UserRoleEnterprise u ", "order by u.id desc ");

        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<UserRoleEnterprise> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, UserRoleEnterprise.class);

        return queryParam.findPageList(typedQuery);
    }

    @Override
    public UserRoleEnterprise findModel(Long id) {
        return entityManager.find(UserRoleEnterprise.class, id);
    }

    @Override
    public Integer add(UserRoleEnterprise model) {
        entityManager.persist(model);
        logger.info("UserRoleEnterpriseDaoImpl添加UserRoleEnterprise成功！");
        return 1;
    }

    @Override
    public Integer update(UserRoleEnterprise model) {
        UserRoleEnterprise userRoleEnterprise = entityManager.find(UserRoleEnterprise.class, model.getId());
        userRoleEnterprise.setEnterprise(model.getEnterprise());
        userRoleEnterprise.setSystemRole(model.getSystemRole());
        userRoleEnterprise.setSystemUser(model.getSystemUser());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        UserRoleEnterprise userRoleEnterprise = entityManager.find(UserRoleEnterprise.class, id);
        entityManager.remove(userRoleEnterprise);
        return 1;
    }

    @Override
    public Integer deleteLogic(UserRoleEnterprise model) {
        return null;
    }
}
